package ucas.pin.web.services;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import ucas.pin.web.domain.Consist;
import ucas.pin.web.domain.Disease;
import ucas.pin.web.domain.MedMaterial;
import ucas.pin.web.domain.Prescription;
import ucas.pin.web.domain.Treat;
import ucas.pin.web.dto.GraphDto;
import ucas.pin.web.dto.LinkDto;
import ucas.pin.web.dto.NodeDto;
import ucas.pin.web.repositories.DiseaseRepository;
@Service
public class DiseaseService {
	@Autowired DiseaseRepository diseaseRepository;
	
	private GraphDto toEchartsFormat(Collection<Disease> coll_des){	
		List<NodeDto> listNode = new ArrayList();
		List<LinkDto> listLink = new ArrayList();
		for(Disease des : coll_des) {
			listNode.add(des.toNodeDto(2));
			for(Treat tre : des.getTreates()) {
				Prescription pre = (Prescription)tre.getStart_Entity();
				listNode.add(pre.toNodeDto(0));
				listLink.add(tre.toLinkDto());
			}
		}
		return new GraphDto(listNode,listLink);
	}
	
	
	public GraphDto graph(String name) {
		Collection<Disease> result = diseaseRepository.getGraphByName(name);
		if(result.isEmpty()) {
			result = diseaseRepository.getEntityByName(name);
		}
		//test_Pre(result);
		System.out.println("get Graph ............"+name);
		return toEchartsFormat(result);
	}
	public Collection<Disease> searchByKeyWord(String name) {
		Collection<Disease> result = diseaseRepository.findDiseaseByNameLike(".*"+name+".*");
		return result;
	}
	
}
